package cn.itcast.web.controller.cargo;


import cn.itcast.domain.ResultInfo;
import cn.itcast.domain.cargo.PackingList;
import cn.itcast.domain.cargo.ShippingOrder;
import cn.itcast.service.cargo.ContractService;
import cn.itcast.service.cargo.PackingListService;
import cn.itcast.service.cargo.ShippingOrderService;
import cn.itcast.web.controller.BaseController;
import cn.itcast.web.utils.BeanMapUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhou
 * @since 2021-01-28
 */
@Controller
@RequestMapping("/cargo/shipping")
public class ShippingOrderController extends BaseController {

    @Reference
    private ContractService contractService;

    @Reference
    private ShippingOrderService shippingOrderService;

    @Reference
    private PackingListService packingListService;



    @RequestMapping("/save.do")
    public String save(ShippingOrder shippingOrder){
        shippingOrder.setCompanyId(getLoginUserCompanyId());
        shippingOrder.setCreateDept(getLoginUser().getDeptId());

        shippingOrderService.save(shippingOrder);
        return "redirect:/cargo/shipping/list.do";
    }

    @RequestMapping("list.do")
    public String list(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "5") Integer pageSize){
        //获取companyId
        String loginUserCompanyId = getLoginUserCompanyId();
        //根据companyId查询该公司所有装箱信息
        PageInfo<ShippingOrder> pageInfo = shippingOrderService.findByPage(pageNum,pageSize,loginUserCompanyId);

        //保存到请求域
        request.setAttribute("pageInfo",pageInfo);

        //返回到list页面
        return "cargo/shipping/shipping-list";
    }

//    /cargo/shipping/toView.do?id="+id
//    @RequestMapping("/toView.do")
//    public String toView(@RequestParam("id") String shippingListId){
//        PackingList shippingList = shippingOrderService.getById(shippingListId);
//
//        request.setAttribute("shipping",shippingList);
//
//        return "cargo/shipping/shipping-view";
//    }

    @RequestMapping("/delete.do")
    public String delete(@RequestParam("id") String shippingOrderId){
        ResultInfo resultInfo = shippingOrderService.delete(shippingOrderId);

        if (resultInfo.getSuccess()){
            return "redirect:/cargo/shipping/list.do";
        }else {
            request.setAttribute("error",resultInfo.getMsg());
            return "forward:/error-info.jsp";
        }
    }

    @RequestMapping("/invoice.do")
    public String invoice(@RequestParam("id") String shippingOrderId){
        ResultInfo resultInfo = shippingOrderService.invoice(shippingOrderId);

        if (resultInfo.getSuccess()){
            //若结果为true，data存储到请求域中
            request.setAttribute("invoice", resultInfo.getData());
//            request.setAttribute("marks", ((List<Object>) resultInfo.getData()).get(1));

            //跳转到添加委托信息页
            return "cargo/shipping/shipping-invoice";
        }else {
            //若结果为fasle，跳转到失败页
            request.setAttribute("error",resultInfo.getMsg());
            return "forward:/error-info.jsp";
        }
    }

    @ResponseBody
    @RequestMapping("/exportPdf")
    public void exportPdf(String id) throws Exception {
        //通知浏览器以附件下载的形式处理
        response.setHeader("content-disposition","attachment;filename=shipping.pdf");

        //1. 获取到模板的输入流
        InputStream inputStream = session.getServletContext().getResourceAsStream("/jasper/shipping_order.jasper");

        //2. 根据id查找报运单
        ShippingOrder ship = shippingOrderService.findById(id);
        PackingList packing = packingListService.findById(id);
        ship.setIsBatch(transferValue(ship.getIsBatch()));
        ship.setIsTrans(transferValue(ship.getIsTrans()));

        //3. 把报运单的对象转换为map
        Map<String, Object> map = BeanMapUtils.beanToMap(ship);
        Map<String, Object> packingMap = BeanMapUtils.beanToMap(packing);
        map.putAll(packingMap);

        // 创建javabena数据源对象
        List<Map<String,Object>> list = new ArrayList<>();
        list.add(map);

        //5. 把报运单的商品存储到数据源中
        JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(list);

        JasperPrint jasperPrint = JasperFillManager.fillReport(inputStream, map,dataSource);

        //6. 把pdf输出即可
        JasperExportManager.exportReportToPdfStream(jasperPrint,response.getOutputStream());
    }

    public String transferValue(String flag){
        if ("1".equals(flag))
            return "是";
        else
            return "否";
    }
}


